package com.example.food_contract_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.food_contract_system.domain.Appeal;
import com.example.food_contract_system.domain.Freeze;
import com.example.food_contract_system.mapper.AgencyMapper;
import com.example.food_contract_system.mapper.AppealMapper;
import com.example.food_contract_system.service.FreezeService;
import com.example.food_contract_system.mapper.FreezeMapper;
import com.example.food_contract_system.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

/**
* @author Lhy
* @description 针对表【freeze】的数据库操作Service实现
* @createDate 2023-07-13 15:15:50
*/
@Service
public class FreezeServiceImpl extends ServiceImpl<FreezeMapper, Freeze> implements FreezeService{

    @Autowired
    private FreezeMapper freezeMapper;
    @Autowired
    private AgencyMapper agencyMapper;
    @Autowired
    private AppealMapper appealMapper;

    @Transactional
    @Override
    public R insertFreeze(Freeze freeze) {

        UUID uuid = UUID.randomUUID();
        String s = uuid.toString();
        freeze.setFId(s.substring(0,4));

        freezeMapper.insert(freeze);

        agencyMapper.updateAccountStatus("1",freeze.getAId());

        return null;
    }

    @Transactional
    @Override
    public R thawAgency(String aId) {
        agencyMapper.updateAccountStatus("0",aId);

        freezeMapper.delete(new UpdateWrapper<Freeze>()
                .eq("a_id",aId));

        return R.ok();
    }

    @Override
    public R getFreezeById(String aId) {
        Freeze freeze = freezeMapper.selectOne(new QueryWrapper<Freeze>()
                .eq("a_id", aId));
        if (freeze != null){
            return R.ok().put("data",freeze);
        }
        return R.error();
    }

    @Transactional
    @Override
    public R thawAgencyApply(String appId) {
        Appeal appeal = appealMapper.selectOne(new QueryWrapper<Appeal>()
                .eq("app_id", appId));

        agencyMapper.updateAccountStatus("0",appeal.getAId());

        freezeMapper.delete(new UpdateWrapper<Freeze>()
                .eq("a_id",appeal.getAId()));

        appealMapper.updateMyself(appId);

        return R.ok();
    }
}




